package Client_logic;

import java.util.Date;

/**
 * This class store activeCampaign details
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil 
 *
 */
public class DealSale{
	private long DealSaleID;
	private Template template = null;
	private Date startDate = null;
	private Date endDate = null;
	private String status = null;

	/**
	 * @param DealSaleID
	 * @param startDate
	 * @param endDate
	 * @param status
	 * @param template
	 */
	public DealSale(long DealSaleID, Date startDate, Date endDate, String status,Template template) {
		this.DealSaleID = DealSaleID;
		this.startDate = startDate;
		this.endDate = endDate;
		this.status = status;
		this.template = template;
	}


	/**
	 * @return the DealSaleID
	 */
	public long getDealSaleID() {
		return DealSaleID;
	}


	/**
	 * @param DealSaleID the DealSaleID to set
	 */
	public void setDealSaleID(long DealSaleID) {
		this.DealSaleID = DealSaleID;
	}


	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}


	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}


	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}


	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}


	/**
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}


	/**
	 * @param status the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}


	/**
	 * @return the template
	 */
	public Template getTemplate() {
		return template;
	}


	/**
	 * @param template the template to set
	 */
	public void setTemplate(Template template) {
		this.template = template;
	}
	
	/**
	 * This function saves a new Deal Sale to DB
	 * @return String on success, null on fail
	 * @throws ClientNotInitializesException
	 */
	public String saveDealSale() throws ClientNotInitializesException{
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			Object ServerMessage = connectiontoserver.saveDealSale(this);
			if (ServerMessage == null)
				return null;
			return (String)ServerMessage;
		}
		catch(Exception e){
			return null;
		}				
	}
	
	/**
	 * This function updates a Deal Sale in the DB
	 * @return String on success, null on fail
	 * @throws ClientNotInitializesException
	 */
	public String updateDealSale() throws ClientNotInitializesException{
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			Object ServerMessage = (Object[])connectiontoserver.updateDealSale(this);
			if (ServerMessage == null)
				return null;
			return (String)ServerMessage;
		}
		catch(Exception e){
			return null;
		}				
	}
	
	/**
	 * This function loads a Deal Sale from DB
	 * @param DealSaleID the ID of Deal Sale to load
	 * @return true on success , false on fail
	 * @throws ClientNotInitializesException
	 */
	public boolean loadDealSale(long DealSaleID) throws ClientNotInitializesException{		
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			Object[] DealSale = (Object[])connectiontoserver.loadDealSale(DealSaleID);
			if (DealSale == null )				
				return false;
			setDealSaleID((Long)(DealSale[0]));
			setStartDate((Date)(DealSale[1]));
			setEndDate((Date)(DealSale[2]));
			setStatus((String)(DealSale[3]));
			if (template == null)
				template = new Template(0,"",null,"","");
			return (template.loadTemplate((Long)DealSale[4]));		
		}
		catch(Exception e){
			return false;
		}				
	}
}
